package com.icesoft.xsnow.upms.service.impl;


import cn.hutool.core.util.ArrayUtil;
import com.icesoft.xsnow.common.core.constant.CommonConstants;
import com.icesoft.xsnow.framework.api.util.ApiAssert;
import com.icesoft.xsnow.framework.service.impl.BaseServiceImpl;
import com.icesoft.xsnow.upms.api.dto.UserInfo;
import com.icesoft.xsnow.upms.api.po.SysRole;
import com.icesoft.xsnow.upms.api.po.SysUser;
import com.icesoft.xsnow.upms.core.enums.UserEcodeEnum;
import com.icesoft.xsnow.upms.mapper.SysUserMapper;
import com.icesoft.xsnow.upms.service.SysPermissionService;
import com.icesoft.xsnow.upms.service.SysRoleService;
import com.icesoft.xsnow.upms.service.SysUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/*
*Copyright © 2019 XSNOW
*四川瑞韬科技有限公司
*http://www.ratotech.com.cn
*All rights reserved.
*/

/**
 * @author xuefeng.gao
 * @program: xsnow
 * @description: 用户表 服务实现类
 * @create: 2019-05-27
 */
@Service
@AllArgsConstructor
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    PasswordEncoder passwordEncoder;

    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    SysPermissionService sysPermissionService;

    @Override
    public boolean save(SysUser user) {
        if (!StringUtils.isEmpty(user.getPassword())) {
            passwordEncoder.encode(user.getPassword());
        }
        return super.save(user);
    }

    @Override
    public boolean updateById(SysUser user) {
        if (!StringUtils.isEmpty(user.getPassword())) {
            passwordEncoder.encode(user.getPassword());
        }
        return super.updateById(user);
    }

    @Override
    @Transactional
    public void resetPwd(Integer uid) {
        SysUser user = getById(uid);
        ApiAssert.notNull(UserEcodeEnum.USER_NOT_FOUND, user);
        user.setPassword(passwordEncoder.encode(CommonConstants.DEFAULT_USER_PASSWORD));
        updateById(user);
    }

    @Override
    public UserInfo findUserInfo(SysUser user) {
        UserInfo userInfo = new UserInfo();
        userInfo.setSysUser(user);
        //设置角色列表  （ID）
        List<Integer> roleIds = sysRoleService.findRolesByUserId(user.getId())
                .stream()
                .map(SysRole::getId)
                .collect(Collectors.toList());
        userInfo.setRoles(ArrayUtil.toArray(roleIds, Integer.class));

        //设置权限列表（menu.permission）
        Set<String> permissions = new HashSet<>();
        permissions.addAll(sysPermissionService.findPermissionsByRoleIds(roleIds));
        userInfo.setPermissions(ArrayUtil.toArray(permissions, String.class));

        //查询角色信息
        return userInfo;
    }
}
